﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using InstructionPointer = System.Collections.Generic.IEnumerator<int>;

namespace IWMAG.Scripting.Interpreter {
    static class Entrance {
        public static void Main() {
            ScriptEngine engine = new ScriptEngine();
            engine.Start(new Func<ScriptEngine, IEnumerable<int>>(F1));
            engine.Start(new Func<ScriptEngine, IEnumerable<int>>(F2));

            // this is emulating the game loop
            while (engine.HasOpenContexts) {
                engine.Update(0.01f);
                Thread.Sleep(10);
            }

            Console.WriteLine();
            Console.Write("All scripts have successfully terminated. ");
            Console.ReadKey();
        }

        public static IEnumerable<int> F1(ScriptEngine engine) {
            Console.WriteLine("F1  > Hello world");
            yield return 1000;
            for (int i = 0; i < 10; i++) {
                Console.WriteLine("F1  > {0}", i * 1000);
            }
            yield return 5000;
            Console.WriteLine("F1  > Shutting down");
        }

        public static IEnumerable<int> F2(ScriptEngine engine) {
            Console.WriteLine("F 2 > Hello world");
            for (int i = 0; i < 26; i++) {
                if (i != 18) {
                    Console.WriteLine("F 2 > {0}", (char)('A' + i));
                    yield return i * 100;
                } else {
                    Console.WriteLine("F 2 > Surprise!");
                    engine.Start(new Func<ScriptEngine, IEnumerable<int>>(F1));
                    engine.Start(new Func<ScriptEngine, IEnumerable<int>>(F3));
                }
            }
            Console.WriteLine("F 2 > Shutting down");
        }

        public static IEnumerable<int> F3(ScriptEngine engine) {
            Console.WriteLine("F  3> Hello world");
            yield return 2500;
            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < i; j++) {
                    Console.WriteLine("F  3> SPAM");
                    yield return 10;
                }
                yield return 750;
            }
            Console.WriteLine("F  3> Shutting down");
        }
    }
}
